package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.CommentDao;
import com.qfedu.mtl.entity.Comments;
import com.qfedu.mtl.query.CommentQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.DateUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentDaoImpl implements CommentDao {

    @Override
    public List<Comments> selectData(Integer page, Integer size, CommentQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_comments where 1=1 ");
        if (!StrUtil.isNullOrEmpty(query.getGoodName())) {
            sqlBuilder.append(" and  good_name =? ");
            params.add(query.getGoodName());
        }
        if (query.getCommentType() != null) {
            sqlBuilder.append(" and comment_type = ? ");
            params.add(query.getCommentType());
        }
        if (query.getReplyStatus() != null) {
            sqlBuilder.append(" and reply_status = ? ");
            params.add(query.getReplyStatus());
        }
        sqlBuilder.append(" limit ?,?  ");
        params.add((page - 1) * size);
        params.add(size);
        return DBUtil.executeDQL(sqlBuilder.toString(), new BeanListHandler<>(Comments.class, true), params.toArray());
    }

    @Override
    public Integer selectTotal(CommentQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_comments where 1=1 ");
        if (!StrUtil.isNullOrEmpty(query.getGoodName())) {
//            like concat('%',?,'%') "
            sqlBuilder.append(" and  good_name = ? ");
            params.add(query.getGoodName());
        }
        if (query.getCommentType() != null) {
            sqlBuilder.append(" and comment_type = ? ");
            params.add(query.getCommentType());
        }
        if (query.getReplyStatus() != null) {
            sqlBuilder.append(" and reply_status = ? ");
            params.add(query.getReplyStatus());
        }
        return DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray()).intValue();
    }

    @Override
    public int insert(Comments com) {
        String sql = "insert into tb_comments(good_id,good_name,order_item_id,user_id,is_anonymous,comment_type," +
                "comment_level,comment_content,comment_imgs,sepc_name,reply_status,reply_content,reply_time,is_show)" +
                " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        return DBUtil.executeDML(sql, DBUtil.getConn(), com.getGoodId(), com.getGoodName(), com.getOrderItemId(),
                com.getUserId(), com.getIsAnonymous(), com.getCommentType(), com.getCommentLevel(), com.getCommentContent(),
                com.getCommentImgs(), DateUtil.date2Str(new Date()), com.getReplyStatus(), com.getReplyContent(), DateUtil.date2Str(new Date()), com.getIsShow());
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from tb_comments where comment_id=?";
        return DBUtil.executeDML(sql, DBUtil.getConn(), id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        String sql = "delete from tb_comments where comment_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if (i != ids.size() - 1) {
                sql += "  ?, ";
            } else {
                sql += "  ?)  ";
            }
        }
        return DBUtil.executeDML(sql, DBUtil.getConn(), ids.toArray());
    }

    @Override
    public int update(Comments com) {
        String sql = "update  tb_comments set good_id=?,good_name=?,order_item_id=?,user_id=?,is_anonymous=?,comment_type=?," +
                "comment_level=?,comment_content=?,comment_imgs=?,sepc_name=?,reply_status=?,reply_content=?,reply_time=?,is_show=? where comment_id=?";
        return DBUtil.executeDML(sql, DBUtil.getConn(), com.getGoodId(), com.getGoodName(), com.getOrderItemId(), com.getUserId(), com.getIsAnonymous(),
                com.getCommentType(), com.getCommentLevel(), com.getCommentContent(), com.getCommentImgs(), DateUtil.date2Str(new Date()), com.getReplyStatus(),
                com.getReplyContent(), DateUtil.date2Str(new Date()), com.getIsShow(), com.getCommentId());
    }

    @Override
    public Comments findById(Integer id) {
        String sql = "select*from tb_comments where comment_id=?";
        return DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanHandler<>(Comments.class, true), id);
    }
}
